package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.NewFactionListEntity;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.NewHttpCofig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.LoginActivity;
import com.shaoshaohuo.app.ui.MainTabActivity;
import com.shaoshaohuo.app.utils.LoginUtils;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/5.
 */

public class Newclassificationshaopdataadapter extends MyBaseAdapter<NewFactionListEntity.DataBean> implements View.OnClickListener {

    private final LayoutInflater from;
    private final Path path;
    private final PathMeasure pathMeasure;
    private Handler handler = new Handler();
    private View view;

    public Newclassificationshaopdataadapter(Context ctx, List<NewFactionListEntity.DataBean> list, boolean flag) {
        super(ctx, list, flag);

        from = LayoutInflater.from(ctx);
        path = new Path();
        pathMeasure = new PathMeasure();
    }

    @Override
    public View getView(int pos, View view, ViewGroup group) {

        final Myholder myholder;
        if (view == null) {
            myholder = new Myholder();
            view = from.inflate(R.layout.factiontowlayout, null);
            myholder.iv_shoppic = (ImageView) view.findViewById(R.id.iv_shoppic);

            int i = context.getResources().getDisplayMetrics().widthPixels / 4;
            myholder.iv_shoppic.getLayoutParams().width = i;
            myholder.iv_shoppic.getLayoutParams().height = i;
            myholder.iv_z = (ImageView) view.findViewById(R.id.iv_shopz);
            myholder.tv_shaopname = (TextView) view.findViewById(R.id.tv_shoppname);
            myholder.tv_shopdate = (TextView) view.findViewById(R.id.tv_shopdate);
            myholder.tv_shopmoney = (TextView) view.findViewById(R.id.tv_shopmoney);
            myholder.tv_new_sort = (TextView) view.findViewById(R.id.tv_new_sort);
            myholder.tv_level = (TextView) view.findViewById(R.id.tv_level);
            myholder.sort_saled_tv = (TextView) view.findViewById(R.id.sort_saled_tv);
            view.setTag(myholder);
        } else {
            myholder = (Myholder) view.getTag();
        }

        NewFactionListEntity.DataBean factionThree = list.get(pos);
        String imgUrl = factionThree.getImgUrl();
        String catname = factionThree.getProName();
        String detail = factionThree.getDetail();
        String money = factionThree.getPrice();
        myholder.tv_shopdate.setText("一件起售 " + detail + "");
        myholder.tv_shaopname.setText(catname);
        if (factionThree.getBuyNum() != null) {
            myholder.sort_saled_tv.setText("已售:" + factionThree.getBuyNum());
        }
        if (catname.length() >= 7) {
            LinearLayout parent = (LinearLayout) myholder.tv_shaopname.getParent();
            parent.setOrientation(LinearLayout.VERTICAL);
        } else {
            LinearLayout parent = (LinearLayout) myholder.tv_shaopname.getParent();
            parent.setOrientation(LinearLayout.HORIZONTAL);
        }
        if (factionThree.getMbn_detailsType() == 1) {
            myholder.tv_shopmoney.setText("￥" + money + "/斤");
        } else if (factionThree.getMbn_detailsType() == 2) {
            myholder.tv_shopmoney.setText("￥" + money + "/个");
        }
        myholder.tv_level.setText(list.get(pos).getLevel());
        Picasso.with(context).load(imgUrl).placeholder(R.mipmap.zhanzhanzhan).into(myholder.iv_shoppic);
        myholder.iv_z.setTag(pos);
        myholder.iv_z.setOnClickListener(this);
        myholder.tv_new_sort.setText(factionThree.getMbn_details());
        return super.getView(pos, view, group);
    }

    @Override
    public void onClick(View v) {
        if(LoginUtils.IsLogining()){
            int postion = (int) v.getTag();
            String catid = list.get(postion).getCatid();
            Map<String, String> headerParams = RequestParam.getHeaderParams();
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("catid", catid);
            Okhttputils.Instanse(context).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + NewHttpCofig.App_CarAdd, stringStringHashMap, new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {
                    Log.e("TAG", "error: 请求失败");
                }

                @Override
                public void error(Call call, int code) {

                }

                @Override
                public void suseff(Call call, Response response, Object o) {
                    Log.e("TAG", "error: 请求成功");
                    Log.e("TAG", "error: 请求成功");
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            MainTabActivity mainTabActivity = (MainTabActivity) context;
                            mainTabActivity.setnum();
                        }
                    });

                }
            }, BaseEntity.class);
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            int statusBarHeight = BaseActivity.getStatusBarHeight(v.getContext());
            Log.d("TAG", "onClick: statusBarHeight" + statusBarHeight + "---y---" + y);
            path.reset();
            float v1 = (y - statusBarHeight);
            path.moveTo(v.getLeft() + v.getWidth() / 2, v1);
            int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
            int heightPixels = context.getResources().getDisplayMetrics().heightPixels;
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.shouye_weixuan);
            int height = bitmap.getHeight();
            path.quadTo(widthPixels / 2, (heightPixels - v1) / 2 + v1, widthPixels / 2 + bitmap.getWidth(),
                    heightPixels - height / 2);
            pathMeasure.setPath(path, false);
            if (adapterListener != null) {
                adapterListener.pathmove(pathMeasure, v.getLeft() + v.getWidth() / 2, v1);
            }
        }else {
            context.startActivity(new Intent(context, LoginActivity.class));
        }



    }

    class Myholder {
        ImageView iv_shoppic, iv_z;
        TextView tv_shaopname, tv_shopmoney, tv_shopdate, tv_new_sort, tv_level, sort_saled_tv;
    }

    private AdapterListener adapterListener;

    public void setAdapterListener(AdapterListener adapterListener) {
        this.adapterListener = adapterListener;
    }

    public interface AdapterListener {
        void pathmove(PathMeasure pathMeasure, float x, float y);
    }
}
